package apis

import (
	"errors"
	"gitee.com/sansaniot/ssiot-core/httpmvc/api"
	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"gitee.com/sansaniot/ssiot-core/serrors"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"ssadmin/common/constant"
	"ssadmin/internal/admin/models"
	"ssadmin/internal/admin/models/command"
	"ssadmin/internal/admin/models/query"
	"ssadmin/internal/admin/service"
)

type SysDictType struct {
	api.Api
}

func (e SysDictType) GetPage(c *gin.Context) {
	s := service.SysDictType{}
	req := query.SysDictTypeGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	list := make([]models.SysDictType, 0)
	var count int64
	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Page(count, req.PageSize, req.PageIndex, list)
}

func (e SysDictType) Get(c *gin.Context) {
	s := service.SysDictType{}
	req := query.SysDictTypeGetReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	var object models.SysDictType
	err = s.Get(&req, &object)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(object)
}

func (e SysDictType) Insert(c *gin.Context) {
	s := service.SysDictType{}
	req := command.SysDictTypeInsertReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetCreateBy(userId)
	err = s.Insert(&req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}

func (e SysDictType) Update(c *gin.Context) {
	s := service.SysDictType{}
	req := command.SysDictTypeUpdateReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Fail(serrors.Fail)
		e.Logger.Error(err)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetUpdateBy(userId)
	err = s.Update(&req)
	if err != nil {
		e.Logger.Error(err)
		return
	}
	e.Data(req.GetId())
}

func (e SysDictType) Delete(c *gin.Context) {
	s := service.SysDictType{}
	req := command.SysDictTypeDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.JSON, nil).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	userId := user.Get(c, constant.CacheKeyUserId).(string)
	req.SetUpdateBy(userId)
	err = s.Remove(&req)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(req.GetId())
}

func (e SysDictType) GetAll(c *gin.Context) {
	s := service.SysDictType{}
	req := query.SysDictTypeGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req, binding.Form).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(serrors.Fail)
		return
	}
	list := make([]models.SysDictType, 0)
	err = s.GetAll(&req, &list)
	if err != nil {
		e.Fail(serrors.Fail)
		return
	}
	e.Data(list)
}

// 变量导出
func (e SysDictType) ExportDictType(c *gin.Context) {
	req := query.SysDictTypeGetPageReq{}
	// 业务service
	s := &service.SysDictType{}
	// 模型
	m := models.SysDictType{}
	// 上下文
	err := e.MakeContext(c).Bind(&req).MakeService(&s.Service).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	if len(req.SystemId) == 0 {
		err = errors.New("systemId 不能为空")
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 注入模型
	// 执行
	//req.LoadUser(c)
	err = s.ExportDictType(c, &m, &req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	c.Abort()
}

// 变量导入
func (e SysDictType) ImportDictType(c *gin.Context) {
	req := query.SysDictTypeGetPageReq{}
	// 业务service
	s := &service.SysDictType{}
	// 模型
	m := models.SysDictType{}
	// 上下文
	err := e.MakeContext(c).Bind(&req, binding.FormMultipart).MakeService(&s.Service).MakeModel(&m.Model).Errors
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	if len(req.SystemId) == 0 {
		err = errors.New("systemId 不能为空")
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 注入模型
	//s.SetVariableModel(&m)
	// 执行
	//req.LoadUser(c)
	err = s.ImportDictType(c, &m, &req)
	if err != nil {
		e.Logger.Error(err)
		e.Fail(0, err.Error())
		return
	}
	// 返回
	e.Ok(nil)
}
